package com.itmg.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

/**
 * 一个工具类
 * @author Jason
 *
 */
public class StringUtil {
	/**
	 * 文字替换
	 * 
	 * @param source
	 *            原文本
	 * 
	 * @param oldString
	 *            文本中需要替换的文字
	 * @param newString
	 *            替换后的新文字
	 * 
	 * @return 替换后的文本
	 */
	public static String replace(String source, String oldString,
			String newString) {
		if (source == null)
			return null;
		StringBuffer output = new StringBuffer();
		int lengOfsource = source.length();
		int lengOfold = oldString.length();
		int posStart = 0;
		int pos;
		while ((pos = source.indexOf(oldString, posStart)) >= 0) {
			output.append(source.substring(posStart, pos));
			output.append(newString);
			posStart = pos + lengOfold;
		}
		if (posStart < lengOfsource) {
			output.append(source.substring(posStart));
		}
		return output.toString();
	}

	/**
	 * 把文本，按某各字符串分割成 段 存放在ArrayList中
	 * 
	 * 
	 * @param source
	 *            原文本
	 * 
	 * @param split
	 *            文本中分割字符串 *
	 * @return 分解后
	 */
	public static ArrayList<String> splitStr(String source, String split) {
		ArrayList<String> arrry = new ArrayList<String>();
		int fromIndex = 0;
		boolean endwithSplit = false;
		int index = source.indexOf(split, fromIndex);
		while (index >= 0) {
			if (fromIndex < index) {
				String part = source.substring(fromIndex, index);
				arrry.add(part);
			}
			fromIndex = index + split.length();
			if (fromIndex > source.length() - 1) {
				endwithSplit = true;
				break;
			}
			index = source.indexOf(split, fromIndex);
		}
		if (!endwithSplit) {
			String endPart = source.substring(fromIndex);
			arrry.add(endPart);
		}
		return arrry;
	}

	/**
	 * 字符串中 全角 变为半角
	 */
	public static String ToBanJiao(String input) {
		if (input == null)
			return null;
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}

	/**
	 * 半角字符转化为全角字符
	 */
	public static String ToQuanJiao(String input) {
		// 半角转全角：
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 32) {
				c[i] = (char) 12288;
				continue;
			}
			if (c[i] < 127)
				c[i] = (char) (c[i] + 65248);
		}
		return new String(c);
	}

	/**
	 * 替换字符串中的全角字符
	 */
	public static String replace(String line) {
		if (line == null || "".equals(line))
			return line;
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("，", ",");
		map.put("。", ".");
		map.put("〈", "<");
		map.put("〉", ">");
		map.put("‖", "|");
		map.put("《", "<");
		map.put("》", ">");
		map.put("〔", "[");
		map.put("〕", "]");
		map.put("﹖", "?");
		map.put("？", "?");
		map.put("“", "\"");
		map.put("”", "\"");
		map.put("：", ":");
		map.put("、", ",");
		map.put("（", "(");
		map.put("）", ")");
		map.put("【", "[");
		map.put("】", "]");
		map.put("—", "-");
		map.put("～", "~");
		map.put("！", "!");
		map.put("‵", "'");
		map.put("①", "1");
		map.put("②", "2");
		map.put("③", "3");
		map.put("④", "4");
		map.put("⑤", "5");
		map.put("⑥", "6");
		map.put("⑦", "7");
		map.put("⑧", "8");
		map.put("⑨", "9");
		map.put("　", " ");
		map.put("*", "\\*");
		map.put("?", "\\?");
		map.put("}", "\\}");
		map.put("{", "\\{");
		map.put("!", "\\!");
		map.put("&", "\\&");
		map.put("^", "\\^");
		map.put("/", "\\/");
		map.put("\\", "\\\\");
		int length = line.length();
		for (int i = 0; i < length; i++) {
			String charat = line.substring(i, i + 1);
			if (map.get(charat) != null) {
				line = line.replace(charat, (String) map.get(charat));
			}
		}
		line = ToBanJiao(line);
		return line;
	}

	/**
	 * 过虑查询出的数据中的HTML 换行标志代码
	 * 
	 * @param in
	 *            原始文字
	 * @return 过虑后的文字
	 */
	public static String filterHtml(String in) {
		if (in == null)
			return "";
		in = replace(in, "<br>", "");
		in = replace(in, "<br />", "");
		in = replace(in, "<br/>", "");
		in = replace(in, "<br", "");
		in = replace(in, "<b", "");
		return in;
	}

	/**
	 * 过虑查询出的数据中的HTML 空字符，换行，左右尖括号 标志代码
	 * 
	 * @param in
	 *            原始文字
	 * @return 过虑后的文字
	 */
	public static String delHtmlSpace(String in) {
		if (in == null)
			return "";
		in = replace(in, "&nbsp;", "");
		in = replace(in, "null", "");
		in = replace(in, "<br>", "");
		in = replace(in, "&lt;", "＜");
		in = replace(in, "&gt;", "＞");
		return in;
	}

	public static String replaceHtml(String input) {
		if (input == null)
			return input;
		input = input.toLowerCase();
		input = replace(input, "&lt;", "<");
		input = replace(input, "&gt;", ">");
		input = input.replaceAll("<.*?>", "");
		input = input.replaceAll("&nbsp;", "");
		input = input.replaceAll("null", "");
		return input;
	}

	/**
	 * 格式化整数为三为一分格式
	 * 
	 * @param in
	 *            需要格式化的整数
	 * 
	 * @return 格式后化整数
	 */
	@SuppressWarnings("unused")
	private String format(int in) {
		java.text.DecimalFormat format = new java.text.DecimalFormat("#,##0");
		return format.format(in);
	}

	/**
	 * 替换关键字中的or和and
	 * 
	 * @param keywords
	 *            需要替换的关键字
	 * 
	 * @return 替换后的关键字
	 */
	public static String ignoreReplaceLucene(String keywords) {
		if (keywords == null)
			return null;
		String keyword = keywords;
		keyword = keyword.replaceAll("\\s(?i)or\\s", " OR ");
		keyword = keyword.replaceAll("\\s(?i)and\\s", " AND ");
		if (keyword.indexOf(" OR ") != -1 || keyword.indexOf(" AND ") != -1) {
			return keyword;
		}
		return keywords;
	}

	/**
	 * 去除关键字中的没用的字符
	 * 
	 * @param keywords
	 *            需要替换的关键字
	 * 
	 * @return 替换后的关键字
	 * 
	 * */
	public static String replaceLuceneChar(String keywords) {
		if (keywords == null || "".equals(keywords)) {
			return keywords;
		} else if (keywords.trim().length() == 1) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("+", "\\+");
			map.put("-", "\\-");
			map.put("[", "\\[");
			map.put("]", "\\]");
			map.put("(", "\\(");
			map.put(")", "\\)");
			map.put("~", "\\~");
			map.put(":", "\\:");
			map.put("*", "\\*");
			map.put("?", "\\?");
			map.put("}", "\\}");
			map.put("{", "\\{");
			map.put("!", "\\!");
			map.put("&", "\\&");
			map.put("^", "\\^");
			map.put("/", "\\/");
			map.put("\\", "\\\\");
			int length = keywords.length();
			for (int i = 0; i < length; i++) {
				String charat = keywords.substring(i, i + 1);
				if (map.get(charat) != null) {
					keywords = keywords.replace(charat, (String) map
							.get(charat));
				}
			}
		}
		keywords = ToBanJiao(keywords);
		return keywords;
	}

	// 去掉joburl中的/coojin 或者 /Coojin
	public static String filterJoburl(String joburl) {
		if (joburl == null)
			return joburl;
		joburl = joburl.replace("/coojin", "");
		joburl = joburl.replace("/Coojin", "");
		return joburl;
	}

	/**
	 * 格式化数字，让它保证共9位数字，用于范围查询 如把 100 格式化后成 000000100
	 * 
	 * @param number
	 *            新的数据
	 * @return 化后成的数据
	 * */
	public static String formatNumbers(String number) {
		StringBuffer sb = new StringBuffer();
		int length = number.length();
		if (length <= 9) {
			for (int i = length; i < 9; i++) {
				sb.append("0");
			}
			sb.append(number.toString());
		}
		return sb.toString();
	}

	/**
	 * 过虑查询出的数据中的HTML 代码空字符，换行，左右尖括号 标志代码
	 * 
	 * @param in
	 *            原始文字
	 * @return 过虑后的文字
	 */
	public static String replaceHtmlCode(String input) {
		if (input == null)
			return input;
		input = input.toLowerCase();
		input = replace(input, "&lt;", "<");
		input = replace(input, "&gt;", ">");
		input = input.replaceAll("<.*?>", "");
		input = input.replaceAll("&nbsp;", "");
		input = input.replaceAll("null", "");
		return input;
	}
	
	public static boolean checkIsEmpty(String str){
	    return str == null || str.length() == 0;
	}
	
	@SuppressWarnings("deprecation")
	public static Timestamp getQianSanTime(Timestamp time){
		Calendar calendar = Calendar.getInstance(); 
		calendar.add(Calendar.DATE, -3);    //得到前一天 
		int year = calendar.get(Calendar.YEAR); 
		int month = calendar.get(Calendar.MONTH); 
		int day = calendar.get(Calendar.DATE);
		Timestamp ti = new Timestamp(year-1900,month,day,0,0,0,0);
		return ti;
	}
}
